#import data
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(ggridges)
weather_df =
rnoaa::meteo_pull_monitors( ##download NOAA weather data
c("USW00094728", "USC00519397", "USS0023B17S"),
var = c("PRCP", "TMIN", "TMAX"),
date_min = "2017-01-01",
date_max = "2017-12-31") %>%
mutate(
name = recode(
id,
USW00094728 = "CentralPark_NY",
USC00519397 = "Waikiki_HA",
USS0023B17S = "Waterhole_WA"),
tmin = tmin / 10,
tmax = tmax / 10) %>%
select(name, id, everything())
## Registered S3 method overwritten by 'hoardr':
## method from
## print.cache_info httr
## using cached file: ~/Library/Caches/R/noaa_ghcnd/USW00094728.dly
## date created (size, mb): 2022-09-29 10:32:05 (8.401)
## file min/max dates: 1869-01-01 / 2022-09-30
## using cached file: ~/Library/Caches/R/noaa_ghcnd/USC00519397.dly
## date created (size, mb): 2022-09-29 10:32:09 (1.699)
## file min/max dates: 1965-01-01 / 2020-03-31
## using cached file: ~/Library/Caches/R/noaa_ghcnd/USS0023B17S.dly
## date created (size, mb): 2022-09-29 10:32:11 (0.95)
## file min/max dates: 1999-09-01 / 2022-09-30
Let’s make a scatter plot.
ggplot(weather_df, aes(x = tmin, y = tmax))
only make a plot.
ggplot(weather_df, aes(x = tmin, y = tmax)) +
geom_point()
## Warning: Removed 15 rows containing missing values (geom_point).
Let’s make a same scatterplot, but different.
weather_df %>%
drop_na %>%
filter(name == "CentralPark_NY") %>%
ggplot(aes(x = tmin, y = tmax)) +
geom_point()
Let’ keep making a same scatterplot, but different. (the plot is saved.)
plot_weather =
weather_df %>%
ggplot(aes(x = tmin, y = tmax))
plot_weather + geom_point()
## Warning: Removed 15 rows containing missing values (geom_point).
ggplot(weather_df, aes(x = tmin, y = tmax)) +
geom_point(aes(color = name)) +
geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## Warning: Removed 15 rows containing non-finite values (stat_smooth).
## Warning: Removed 15 rows containing missing values (geom_point).
按名字区分color 仅在点图中, 因为只在geom_point()
ggplot(weather_df, aes(x = tmin, y = tmax, color = name)) +
geom_point(aes()) +
geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 15 rows containing non-finite values (stat_smooth).
## Warning: Removed 15 rows containing missing values (geom_point).
write ‘color’ when making plot (the first step), it will be followed in
every graphs.
ggplot(weather_df, aes(x = tmin, y = tmax, color = name)) +
geom_point(alpha = .3) +
geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 15 rows containing non-finite values (stat_smooth).
## Warning: Removed 15 rows containing missing values (geom_point).
‘alpha’ make the graph transparent,
make separate panels. (by facet_grid)
ggplot(weather_df, aes(x = tmin, y = tmax, color = name)) +
geom_point(alpha = .3) +
geom_smooth(se = FALSE) +
facet_grid(. ~name)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 15 rows containing non-finite values (stat_smooth).
## Warning: Removed 15 rows containing missing values (geom_point).
纵向
ggplot(weather_df, aes(x = tmin, y = tmax, color = name)) +
geom_point(alpha = .3) +
geom_smooth(se = FALSE) +
facet_grid(name ~.)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 15 rows containing non-finite values (stat_smooth).
## Warning: Removed 15 rows containing missing values (geom_point).
横向
‘tmax’ vs ‘tmin’ is boring, let’s spice it up some.
ggplot(weather_df, aes(x = date, y = tmax, color = name)) +
geom_point(aes(size = prcp), alpha = .5) +
geom_smooth(se = FALSE) +
facet_grid(. ~ name) +
theme(axis.text.x = element_text(angle = 90)) ##rotate the x axis 90'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 3 rows containing non-finite values (stat_smooth).
## Warning: Removed 3 rows containing missing values (geom_point).
ggplot(weather_df, aes(x = tmax, y = tmin)) +
geom_hex()
## Warning: Removed 15 rows containing non-finite values (stat_binhex).
when the point is too dense, use hex
histograms, barplots, boxplots, violins, …
ggplot(weather_df, aes(x = tmax, fill = name)) +
geom_histogram(position = "dodge", binwidth = 2)
## Warning: Removed 3 rows containing non-finite values (stat_bin).
“dodge” separate the bars
MORE OPTIONS!
ggplot(weather_df, aes(x = tmax, fill = name)) +
geom_density(alpha = .4, adjust = .5, color = "blue")
## Warning: Removed 3 rows containing non-finite values (stat_density).
boxplot
weather_df %>%
ggplot(aes(x = name, y = tmax)) + geom_boxplot()
## Warning: Removed 3 rows containing non-finite values (stat_boxplot).
violin plots
ggplot(weather_df, aes(x = name, y = tmax)) +
geom_violin(aes(fill = name), alpha = .5) +
stat_summary(fun = "median", color = "blue")
## Warning: Removed 3 rows containing non-finite values (stat_ydensity).
## Warning: Removed 3 rows containing non-finite values (stat_summary).
## Warning: Removed 3 rows containing missing values (geom_segment).
density ridges
ggplot(weather_df, aes(x = tmax, y = name)) +
geom_density_ridges(scale = .85)
## Picking joint bandwidth of 1.84
## Warning: Removed 3 rows containing non-finite values (stat_density_ridges).
distribution (?scale)
##saving and emdedding plots. First, let’s save a plot.
weather_scatterplot =
weather_df %>%
ggplot(aes(x = date, y = tmax, color = name)) +
geom_point(aes(size = prcp), alpha = .5) +
geom_smooth(se = FALSE) +
facet_grid(. ~ name) +
theme(axis.text.x = element_text(angle = 90))
weather_scatterplot
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 3 rows containing non-finite values (stat_smooth).
## Warning: Removed 3 rows containing missing values (geom_point).
ggsave("./results/weather scatterplot.pdf", weather_scatterplot,
width = 8, height = 5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 3 rows containing non-finite values (stat_smooth).
## Removed 3 rows containing missing values (geom_point).
weather_scatterplot
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 3 rows containing non-finite values (stat_smooth).
## Warning: Removed 3 rows containing missing values (geom_point).